package com.shengwei.truck.driver.view.ui.mine.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.bean.JsonCar;
import com.shengwei.truck.driver.bean.Result;
import com.shengwei.truck.driver.databinding.ActivitySearchCarBinding;
import com.shengwei.truck.driver.view.adapter.mine.CarModelAdapter;
import com.shengwei.truck.driver.view.ui.BaseActivity;
import com.shengwei.truck.driver.vm.CarInfoViewModel;
import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.ArrayList;
import java.util.List;


/**
 * 搜索 “汽车型号(如奥迪A6)”列表
 */
public class SearchCarActivity extends BaseActivity<CarInfoViewModel, ActivitySearchCarBinding> implements View.OnClickListener {
    SearchView.SearchAutoComplete mSearchAutoComplete;
    CarModelAdapter mAdapter;
    String TAG = "MainActivity";
    List<JsonCar> mCarNames = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initLiveDataObersve();
        initData();
    }
    /**
     * 接口调用成功时，监听接口返回的数据
     */
    private void initLiveDataObersve() {

        //监听 "检索车辆名称"接口返回的数据
        mViewModel.getSearchLiveData().observe(this, new Observer<Result<ArrayList<JsonCar>>>() {
            @Override
            public void onChanged(Result<ArrayList<JsonCar>> result) {
                result.handler(new OnCallback<ArrayList<JsonCar>>() {
                    @Override
                    public void onSuccess(ArrayList<JsonCar> data) {
                       // Toast.makeText(SearchCarActivity.this,"检索成功",Toast.LENGTH_SHORT).show();
                        mCarNames.clear();
                        if(data != null && data.size() > 0) {
                            mCarNames.addAll(data);
                        }
                        setAdapter(mAdapter);
                    }
                });
            }
        });
    }
    private void initData() {
        mAdapter = new CarModelAdapter(mCarNames,this);
        //初始化reclerview汽车名称列表
        mDataBinding.lvAutoText.setLayoutManager(new LinearLayoutManager(this));
        mDataBinding.lvAutoText.addItemDecoration(new DefaultItemDecoration(ContextCompat.getColor(this, R.color.line_color)));
        mDataBinding.lvAutoText.setSwipeItemClickListener(mItemClickListener);
        mDataBinding.lvAutoText.setAdapter(mAdapter);
    }
    /**
     * 初始化UI控件
     */
    private void initView() {

        mDataBinding.edtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    mCarNames.clear();
                    mAdapter.notifyDataSetChanged();
                    mDataBinding.lvAutoText.setVisibility(View.GONE);
                    mDataBinding.lvAutoText.setVisibility(View.VISIBLE);


                } else {//有文字则搜索

                    if (mDataBinding.imgBtClear.getVisibility() == View.GONE) {
                        mDataBinding.imgBtClear.setVisibility(View.VISIBLE);
                    }
                    mViewModel.searchCarName(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    mDataBinding.imgBtClear.setVisibility(View.GONE);
                    mCarNames.clear();
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
        mDataBinding.imgBtClear.setOnClickListener(this);
        mDataBinding.imvBack.setOnClickListener(this);
        mDataBinding.tvCancel.setOnClickListener(this);
    }

    /**
     * 汽车名称列表项点击Item事件处理
     */
    private SwipeItemClickListener mItemClickListener = new SwipeItemClickListener() {
        @Override
        public void onItemClick(View itemView, int position) {
            JsonCar jsonCar = mCarNames.get(position);
            String carName = jsonCar.name;
            String carId = jsonCar.id;
            String unit = jsonCar.unit;
            Intent i = new Intent();
            i.putExtra("carId",carId);
            i.putExtra("carName",carName);
            i.putExtra("unit",unit);
            setResult(RESULT_OK,i);
            finish();
        }
    };
    @Override
    public int getContentViewId() {
        return R.layout.activity_search_car;
    }


    /**
     * 清空输入框
     */
    public void clearContent() {
        mDataBinding.edtSearch.setText("");
        mDataBinding.imgBtClear.setVisibility(View.GONE);
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 为列表配置数据
     * @param adapter
     */
    public void setAdapter(RecyclerView.Adapter adapter) {
        if(adapter == null) {
            adapter = new CarModelAdapter(mCarNames,SearchCarActivity.this);
            mDataBinding.lvAutoText.setAdapter(adapter);
        }
        else {
            if(adapter instanceof  CarModelAdapter) {
               // mDataBinding.lvAutoText.setAdapter(adapter);
                ((CarModelAdapter) adapter).notifyDataSetChanged();
            }
        }
        return;
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.imgBtClear){
            clearContent();
        }
        else if(v.getId() == R.id.imvBack){
            finish();
        }
        else if(v.getId() == R.id.tvCancel){
            finish();
        }
    }
}
